package com.example.fufudamowang.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String[] countriesStr ={ "广州", "从化", "武汉", "汕头" };
    private TextView myTextView;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    Animation myAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTextView = (TextView) findViewById(R.id.tv);
        mySpinner = (Spinner) findViewById(R.id.sp);
        adapter = new ArrayAdapter(this,android.R.layout.activity_list_item,countriesStr);
        adapter.setDropDownViewResource(R.layout.myspinner_dropdown);


        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
                myTextView.setText("选择的是" + countriesStr[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView arg0) {

            }

        });
        myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event){
                v.startAnimation(myAnimation);//执行Animation
                v.setVisibility(View.INVISIBLE);//mySpinner隐藏
                return false;

            }
        });
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener()
        {
            public void onFocusChange(View v, boolean hasFocus){

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.spinner_demo1, menu);
        return true;
    }
}
